package com.kotlin.demo.ui.viewmodel

import com.kotlin.demo.constant.Constants
import com.kotlin.demo.data.repository.TranslateRepository
import com.kotlin.demo.helper.DialogHelper
import com.kotlin.demo.tools.ResPathUtils
import com.kotlin.demo.ui.state.SideEffectState
import com.kotlin.demo.ui.state.TranslateIntent
import com.kotlin.demo.ui.state.TranslateState
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import org.koin.core.component.KoinComponent

/**
 * viewModel 帮助类
 */
class TranslateViewModel(
    private val repository: TranslateRepository
) : KoinComponent {
    //翻译结果
    private val _state = MutableStateFlow(TranslateState())
    val state: StateFlow<TranslateState> = _state

    /**
     * 执行意图
     */
    fun processIntent(intent: TranslateIntent) {
        when (intent) {
            is TranslateIntent.TranslateMultilingual -> {//多语言翻译
                translateMultilingual()
            }

            is TranslateIntent.TranslateWrite -> {//写入多语言翻译
                writeTranslate(intent.translateResult)
            }

            is TranslateIntent.TranslateResList -> {//获取多语言路径
                getResPathList(intent.isShowTips, intent.isShowResList)
            }

            is TranslateIntent.TranslateDelResList -> {//删除多语言路径
                delResPathList()
            }
        }
    }

    /**
     * 多语言翻译
     */
    private fun translateMultilingual() {
        if (Constants.translateInput.value.trim() == ("")) {
            DialogHelper.showTipsDialog("翻译内容不能为空!")
            return
        }
        DialogHelper.showLoadingDialog("正在翻译中...请稍后！!")
        Constants.mainScope.launch(Dispatchers.IO) {
            try {
                val data = repository.translateJson(Constants.translateInput.value.trim())
                // 配置 Json 实例以实现格式化
//                _state.value.translateResult ="{}" // 当前不会触发刷新，因为未改变 value state ,
                //这个可以触发刷新 是因为创建了新的state  只有state 改变才会触发刷新，而不是里面的值
//                _state.value = _state.value.copy(translateResult = "{}")
                //emit 是挂起函数，必须在协成中执行，有背压，不会立即执行
                _state.emit(TranslateState(translateResult = data))
                Constants.translateResultText.value = _state.value.translateResult
            } catch (e: Exception) {
                e.printStackTrace()
            } finally {
                DialogHelper.dismissDialog()
            }
        }
    }

    /**
     * 多语言写入
     */
    private fun writeTranslate(result: String) {

    }

    /**
     * 获取多语言路径
     */
    private fun getResPathList(isShowTips: Boolean, isShowResList: Boolean) {
        val list = ResPathUtils.getResList()
        _state.value = _state.value.copy(translateResList = list)
        if (list.isEmpty() && isShowTips) {
            _state.value = _state.value.copy(sideEffectState = SideEffectState.ShowToast("请先添加翻译路径！"))
        }
        if (list.isNotEmpty() && isShowResList) {//是否需要显示路径列表弹窗
            Constants.isExpandResPath.value = true
        }
    }

    /**
     * 删除多语言路径
     */
    private fun delResPathList() {
        ResPathUtils.delResAll()
        DialogHelper.showTipsDialog("删除成功!")
    }
}